package com.jd.order.controller;

import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.dto.OrderDto;
import com.jd.order.query.OrderPetAcquisitionQuery;
import com.jd.order.service.OrderPetAcquisitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    public OrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 接口：添加或修改
     * @param orderPetAcquisition  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if( orderPetAcquisition.getId()!=null) {
                orderPetAcquisitionService.update(orderPetAcquisition);
            } else {
                orderPetAcquisitionService.save(orderPetAcquisition);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
                orderPetAcquisitionService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderPetAcquisition findOne(@PathVariable("id")Long id) {
        return orderPetAcquisitionService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderPetAcquisition> findAll(){
        return orderPetAcquisitionService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) {
        return orderPetAcquisitionService.queryPage(query,request);
    }

    @GetMapping(value="/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancel(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"订单取消失败！");
        }
    }

    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderDto orderDto){
        try {
            orderPetAcquisitionService.confirm(orderDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"确认订单失败！");
        }
    }




}
